//
//  Session.m
//  Normal
//
//  Created by 8b410-2 on 16/12/9.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "Session.h"
#import "AppDelegate.h"
#define KEY_Name_List @"userNameList" // 名字列表
@implementation Session
- (instancetype)init
{
    self = [super init];
    if (self) {
        _user = [self loadUser];
        if (_user) {
            _userStatus = SessionUserStatusLogined;
        }
    }
    return self;
}


+ (instancetype)shareSession
{
    static id session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        session = [[Session alloc] init];
    });
    return session;
}

#define BLUserKey @"UserKey"
- (void)saveUser:(User *)user
{
    if (nil == user) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:BLUserKey];
    }
    else {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:BLUserKey];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (User *)loadUser
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:BLUserKey];
    if (nil == data) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

#pragma mark - 登录相关
#pragma mark 发送登录请求
-(void)userLogin:(NSString *)username pwd:(NSString *)pwd result:(void(^)(NSError *error))result{

}

-(void)userLogin:(NSString *)username md5pwd:(NSString *)pwd result:(void (^)(NSError *))result{

}


#pragma mark 登录成功
- (void)loginSuccess:(User *)user{
    // 登录成功
    _user = user;
    [self saveUser:_user];
    [self updateUserStatus:SessionUserStatusLogined];
}

-(void)updateUser:(User *)user{
    _user = user;
    [self saveUser:_user];
}

#pragma mark - 判断是否已经登录
+ (BOOL)isLogin
{
    return [[Session shareSession] user] != nil;
}

#pragma mark - 更新用户状态
- (void)updateUserStatus:(SessionUserStatus)status
{
    [self willChangeValueForKey:@"userStatus"];
    _userStatus = status;
    [self didChangeValueForKey:@"userStatus"];
}

#pragma mark - 退出登录
- (void)userLogout
{
    [self saveUser:nil];
    _user = nil;
    [self updateUserStatus:SessionUserStatusLogout];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    [cookies enumerateObjectsUsingBlock:^(NSHTTPCookie *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.domain hasSuffix:kCookieUrl]) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:obj];
        }
    }];
    
}
//登录用户名列表
+(void) saveLastLoginUserName:(NSString *)userName
{
    NSArray *list = [Session getDefaultNameList];
    NSMutableArray *nameList = [list mutableCopy];
    if ([nameList indexOfObject:userName] == NSNotFound) {
        
        [nameList insertObject:userName atIndex:0];
    }else {
        [nameList removeObject:userName];
        [nameList insertObject:userName atIndex:0];
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:nameList forKey:KEY_Name_List];
    [defaults synchronize];
}

+(NSString *)getLastLoginUserName{
    NSArray *list = [self getDefaultNameList];
    if (list.count==0) {
        return nil;
    }
    return [list objectAtIndex:0];
}


+(NSArray *) getDefaultNameList
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *nameList = [defaults objectForKey:KEY_Name_List];
    if (nameList == nil) {
        nameList = [[NSArray alloc] init];
    }
    return nameList;
}

@end
